//
//  PKCommunityMainViewController.m
//  PianKe
//
//  Created by ybq on 15/12/21.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKCommunityMainViewController.h"
#import "PKCommunityTopicTableViewController.h"
#import "PKCommunityGroupTableViewController.h"
#import "PKCommunityTopicModel.h"
#import "PKCommunityGroupModel.h"
#import "PKCommunityScrollView.h"

@interface PKCommunityMainViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) PKCommunityTopicTableViewController *topicVC;
@property (nonatomic, strong) PKCommunityGroupTableViewController *groupVC;
@property (nonatomic, strong) NSMutableArray *topicListArray;
@property (nonatomic, strong) NSMutableArray *groupListArray;
@property (nonatomic, strong) NSString *sortType;
@property (nonatomic, strong) UISegmentedControl *topicSegmentControl;
@property (nonatomic, strong) UISegmentedControl *groupSegmentControl;
@property (nonatomic, strong) UIImageView *shortline;
@property (nonatomic, strong) UIButton *topicButton;
@property (nonatomic, strong) UIButton *groupButton;
@property (nonatomic, strong) UIButton *selfButton;

@end

@implementation PKCommunityMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *stateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 20)];
    stateView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:stateView];
    // Do any additional setup after loading the view.
    [self creatUI];
    [self creatVC];
    [self requestTopicData];
    _sortType = @"hot";
    [self requestGroupData];
}




- (void)creatUI {
    
    _topicButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _topicButton.frame = CGRectMake(kWidth / 2 + 30, 10, 20, 20);
    [_topicButton setImage:[UIImage imageNamed:@"topic"] forState:(UIControlStateNormal)];
    [_topicButton addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.navView addSubview:_topicButton];

    _groupButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _groupButton.frame = CGRectMake(_topicButton.frame.origin.x + 60, 10, 20, 20);
    [_groupButton setImage:[UIImage imageNamed:@"group"] forState:(UIControlStateNormal)];
    [_groupButton addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _groupButton.alpha = .3;
    [self.navView addSubview:_groupButton];

    _selfButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _selfButton.frame = CGRectMake(_groupButton.frame.origin.x + 60, 10, 20, 20);
    [_selfButton setImage:[UIImage imageNamed:@"self"] forState:(UIControlStateNormal)];
    [_selfButton addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _selfButton.alpha = .3;
    [self.navView addSubview:_selfButton];
    
    _shortline = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth / 2 + 35, 43, 10, 1)];
    _shortline.backgroundColor = [UIColor blackColor];
    [self.navView addSubview:_shortline];
    
    self.navView.title.text = @"话题";
    // 关闭scrollView自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scrollView = [[PKCommunityScrollView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64)];
    self.scrollView.contentSize = CGSizeMake(kWidth * 3, self.scrollView.frame.size.height);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    
    
    NSArray *itemArray = @[@"Hot", @"New"];
    // 分段按钮
    _topicSegmentControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    _topicSegmentControl.frame = CGRectMake(kWidth / 2 - 80, 5, 160, 25);
    // 设置默认选中 (从零开始)
    _topicSegmentControl.selectedSegmentIndex = 0;
    _topicSegmentControl.tintColor = [UIColor grayColor];
    // 选取ValueChanged 实际上是检测的索引的变化
    [_topicSegmentControl addTarget:self action:@selector(segConAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.scrollView addSubview:_topicSegmentControl];
    [self.view addSubview:self.scrollView];
    
    // 分段按钮
    _groupSegmentControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    _groupSegmentControl.frame = CGRectMake(kWidth / 2 - 80 + kWidth, 5, 160, 25);
    // 设置默认选中 (从零开始)
    _groupSegmentControl.selectedSegmentIndex = 0;
    _groupSegmentControl.tintColor = [UIColor grayColor];
    // 选取ValueChanged 实际上是检测的索引的变化
    [_groupSegmentControl addTarget:self action:@selector(segConAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.scrollView addSubview:_groupSegmentControl];
    [self.view addSubview:self.scrollView];
    
}

- (void)buttonAction:(UIButton *)sender {
    if ([sender isEqual:_topicButton]) {
        if (self.scrollView.contentOffset.x != 0) {
            [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            self.navView.title.text = @"话题";
            CGRect frame = _shortline.frame;
            frame.origin.x = kWidth / 2 + 35;
            [UIView animateWithDuration:.2 animations:^{
                _shortline.frame = frame;
            }];
            _groupButton.alpha = .3;
            _topicButton.alpha = 1;
        }
    } else if ([sender isEqual:_groupButton]) {
        if (self.scrollView.contentOffset.x != kWidth) {
            [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
            self.navView.title.text = @"小组";
            CGRect frame = _shortline.frame;
            frame.origin.x = kWidth / 2 + 95;
            [UIView animateWithDuration:.2 animations:^{
                _shortline.frame = frame;
            }];
            _groupButton.alpha = 1;
            _selfButton.alpha = .3;
            _topicButton.alpha = .3;
            
        }
    } else {
        [self.scrollView setContentOffset:CGPointMake(kWidth * 2, 0) animated:YES];
        self.navView.title.text = @"自己";
        CGRect frame = _shortline.frame;
        frame.origin.x = kWidth / 2 + 155;
        [UIView animateWithDuration:.2 animations:^{
            _shortline.frame = frame;
        }];
        _groupButton.alpha = .3;
        _selfButton.alpha = 1;

    }
}

- (void)segConAction:(UISegmentedControl *)segmentedControl {
    
    if (segmentedControl == _topicSegmentControl) {
        
        switch (segmentedControl.selectedSegmentIndex) {
            case 0:
                _sortType = @"hot";
                [_topicVC.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
                [self requestTopicData];
                break;
            case 1:
                _sortType = @"addtime";
                [_topicVC.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
                [self requestTopicData];
                
            default:
                break;
        }
        
    } else {
        
        switch (segmentedControl.selectedSegmentIndex) {
            case 0:
                _sortType = @"hot";
                [_groupVC.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
                [self requestGroupData];
                break;
            case 1:
                _sortType = @"addtime";
                [_groupVC.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
                [self requestGroupData];
                
            default:
                break;
        }
        
    }
    
    
}

- (void)creatVC {
    _topicVC = [[PKCommunityTopicTableViewController alloc] init];
    _topicVC.tableView.frame = CGRectMake(0, 40, kWidth, self.scrollView.frame.size.height - 40);
    [self addChildViewController:_topicVC];
    [self.scrollView addSubview:_topicVC.tableView];
    
    _groupVC = [[PKCommunityGroupTableViewController alloc] init];
    _groupVC.tableView.frame = CGRectMake(kWidth, 40, kWidth, self.scrollView.frame.size.height - 40);
    [self addChildViewController:_groupVC];
    [self.scrollView addSubview:_groupVC.tableView];
    
}

- (void)requestTopicData {
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"sort"] = _sortType;
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/group/posts_hotlist" parDic:parDic Method:POST finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *infoDic = dataDic[@"data"];
        _topicListArray = [NSMutableArray array];
        for (NSDictionary *dic in infoDic[@"list"]) {
            PKCommunityTopicModel *model = [[PKCommunityTopicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_topicListArray addObject:model];
        }
        _topicVC.listArray = _topicListArray;
        [_topicVC.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)requestGroupData {
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"sort"] = _sortType;
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/group/group_list" parDic:parDic Method:POST finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *infoDic = dataDic[@"data"];
        _groupListArray = [NSMutableArray array];
        for (NSDictionary *dic in infoDic[@"list"]) {
            PKCommunityGroupModel *model = [[PKCommunityGroupModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_groupListArray addObject:model];
        }
        _groupVC.listArray = _groupListArray;
        [_groupVC.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        self.navView.title.text = @"话题";
        CGRect frame = _shortline.frame;
        frame.origin.x = kWidth / 2 + 35;
        [UIView animateWithDuration:.2 animations:^{
            _shortline.frame = frame;
        }];
        _groupButton.alpha = .3;
        _topicButton.alpha = 1;
    } else if (scrollView.contentOffset.x == kWidth) {
        self.navView.title.text = @"小组";
        CGRect frame = _shortline.frame;
        frame.origin.x = kWidth / 2 + 95;
        [UIView animateWithDuration:.2 animations:^{
            _shortline.frame = frame;
        }];
        _groupButton.alpha = 1;
        _selfButton.alpha = .3;
        _topicButton.alpha = .3;
    } else {
        self.navView.title.text = @"自己";
        CGRect frame = _shortline.frame;
        frame.origin.x = kWidth / 2 + 155;
        [UIView animateWithDuration:.2 animations:^{
            _shortline.frame = frame;
        }];
        _groupButton.alpha = .3;
        _selfButton.alpha = 1;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
